/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ArtificialAgent;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import wumpusworld.ControlWindow;
import wumpusworld.MapGenerator;
import wumpusworld.WumpusEngine;

/**
 *
 * @author Macias
 */
public class AgentController implements Runnable{

    public static int speed=600;
    boolean stop = false;
    boolean pause = false;

    ControlWindow cW;
    WumpusEngine wumpusEng;
    ArtificialAgent artificialAgent;

    public AgentController(ControlWindow contWind) {
        this.cW = contWind;
        this.wumpusEng = cW.getWumpusEngine();
        artificialAgent = cW.artificialAgent;

    }

    public void run() {
        wumpusEng.showNotifications = false;
        wumpusEng.disposeWin = false;
        stop=false;
        while(!stop){
//            System.out.println(""+pause);
            if(pause)try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                Logger.getLogger(AgentController.class.getName()).log(Level.SEVERE, null, ex);
            }
            if(!pause){
                pause=cW.checkIfStepWorking.isSelected();
                if(!wumpusEng.isAgentWinner()  && wumpusEng.isAgentAlive()){

                    try {
                        Thread.sleep(speed);
                     }
                    catch (InterruptedException ex) {
                            Logger.getLogger(AgentController.class.getName()).log(Level.SEVERE, null, ex);
                     }


                    int direction = artificialAgent.getPreferredDirection();
                    wumpusEng.move(direction);
                    if(cW.checkIfDynamicLearning.isSelected())artificialAgent.learnFromCurrentPosition();

                    cW.performAfterMoveActions();

                }
                else{
                    try {
                        Thread.sleep(2*speed);
                     }
                    catch (InterruptedException ex) {
                            Logger.getLogger(AgentController.class.getName()).log(Level.SEVERE, null, ex);
                     }

                    if(wumpusEng.isAgentWinner())artificialAgent.winCount++;
                    else artificialAgent.lostCount++;

                    float ratio = (float)artificialAgent.winCount;
                    if(artificialAgent.lostCount!=0) {
                        ratio/=artificialAgent.lostCount;
                        cW.addElementToRatioData(ratio);
                    }
                    else
                        cW.addElementToRatioData(artificialAgent.winCount);

    //                cW.resultsLabel.setText("win/lose ratio: "+artificialAgent.winCount+" / "+ artificialAgent.lostCount + " ("+ratio+")");
                    cW.resultsLabel.setText(String.format("win/lose ratio: %d / %d (%.2f)",artificialAgent.winCount,artificialAgent.lostCount,ratio));
                    cW.plotWindow.setData(cW.getRatioData());
                    cW.plotWindow.repaintCanvas();

                    if(!wumpusEng.isAgentWinner())
                    try {
                        Thread.sleep((int)Math.min((int)(5*speed), 800));
                         }
                        catch (InterruptedException ex) {
                                Logger.getLogger(AgentController.class.getName()).log(Level.SEVERE, null, ex);
                         }

                    cW.performNewGame();
                }



            }
        }
    }

    public void stopThread(){
        this.stop = true;
    }

    public void pauseThread(boolean value){
        this.pause = value;
    }

    


}
